package com.varian.system.controller;

import com.varian.log.annotation.ApiLog;
import com.varian.service.annotation.Resubmit;
import com.varian.service.web.BaseController;
import com.varian.system.dto.SysMenuDTO;
import com.varian.system.dto.SysMenuListDTO;
import com.varian.system.dto.SysMenuTreeDTO;
import com.varian.system.dto.SysMenuWeightsDTO;
import com.varian.system.service.ISysMenuService;
import com.varian.system.vo.SysMenuVO;
import com.varian.tool.result.IR;
import com.varian.tool.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ben
 * @since 2024/7/2
 */
@Resubmit
@RestController
@Tag(name = "菜单控制层")
@RequestMapping("/menu")
public class SysMenuController extends BaseController<ISysMenuService> {

    @ApiLog
    @PostMapping
    @Operation(summary = "新增菜单")
    @PreAuthorize("hasAuthority('menu:add')")
    public IR add(@Validated @RequestBody SysMenuDTO dto) {
        return R.data(baseService.add(dto));
    }

    @ApiLog
    @PutMapping("{id:\\d+}")
    @Operation(summary = "修改菜单")
    @PreAuthorize("hasAuthority('menu:modify')")
    public IR modify(@PathVariable("id") Long id, @Validated @RequestBody SysMenuDTO dto) {
        return R.data(baseService.modify(id, dto));
    }

    @ApiLog
    @GetMapping("{id:\\d+}")
    @Operation(summary = "菜单详情")
    @PreAuthorize("hasAuthority('menu:detail')")
    public R<SysMenuVO> detail(@PathVariable("id") Long id) {
        return R.data(baseService.detail(id));
    }

    @ApiLog
    @DeleteMapping("{id:\\d+}")
    @Operation(summary = "删除菜单")
    @PreAuthorize("hasAuthority('menu:delete')")
    public IR delete(@PathVariable("id") Long id) {
        return R.data(baseService.delete(id));
    }

    @ApiLog
    @GetMapping("tree")
    @Operation(summary = "菜单树列表")
    @PreAuthorize("hasAuthority('menu:tree')")
    public R<List<SysMenuVO>> tree(SysMenuTreeDTO dto) {
        return R.data(baseService.tree(dto));
    }

    @ApiLog
    @GetMapping("list")
    @Operation(summary = "菜单列表")
    @PreAuthorize("hasAuthority('menu:list')")
    public R<List<SysMenuVO>> list(SysMenuListDTO dto) {
        return R.data(baseService.list(dto));
    }

    @ApiLog
    @Operation(summary = "批量修改权重")
    @PutMapping("weights/{parentId:\\d+}")
    @PreAuthorize("hasAuthority('menu:weights')")
    public IR weights(@PathVariable("parentId") Long parentId, @RequestBody List<SysMenuWeightsDTO> list) {
        return R.data(baseService.weights(parentId, list));
    }

    @GetMapping("tree-by-user")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "根据用户获取菜单树列表")
    public R<List<SysMenuVO>> treeByUser() {
        return R.data(baseService.treeByUser());
    }

}
